package rhc.taskSchedule.taskDistribute.impl;

import java.util.Date;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import rhc.discribute.node.host.Host;
import rhc.taskSchedule.BasicComponent;
import rhc.taskSchedule.command.RunningCommandCreater;
import rhc.taskSchedule.commander.TaskScheduleCommanderExecute;
import rhc.taskSchedule.entity.Running;
import rhc.taskSchedule.entity.RunningStartType;
import rhc.taskSchedule.entity.RunningStatus;
import rhc.taskSchedule.entity.RunningType;
import rhc.taskSchedule.exception.RunningException;
import rhc.taskSchedule.taskDistribute.StartRunning;
import rhc.taskSchedule.taskDistribute.TaskDistribute;

public class TaskDistributeImpl extends BasicComponent implements TaskDistribute{

	private TaskScheduleCommanderExecute taskScheduleCommanderExecute;
	
	/**
	 * 用于开始一个任务,任务和调度需要使用不同的开始方式
	 */
	private StartRunning startRunning;
	
	public TaskDistributeImpl(TaskScheduleCommanderExecute taskScheduleCommanderExecute){
		this.taskScheduleCommanderExecute=taskScheduleCommanderExecute;
		startRunning=new BasicStartRunning();
	}
	
	@Override
	public Running createRunning(String sourceId, RunningType runningType, RunningStartType runningStartType)
			throws Exception {
		
		Running running=new Running();
		running.setCreateDate(new Date());
		running.setRunningStartType(runningStartType.type);
		running.setRunningStatus(RunningStatus.CREATE.status);
		running.setRunningType(runningType.type);
		running.setSourceId(sourceId);
		
		superDao.addOneT(running);
		
		return running;
	}

	@Override
	public void startRunning(String runningId) throws Exception {
		Running running=getRunning(runningId);
		
		startRunning.startRunning(running, taskScheduleCommanderExecute);
	}

	@Override
	public void pauseRunning(String runningId) throws Exception {
		Running running=getRunning(runningId);
		Host runningHost=Host.fromPathGetHost(running.getRunNode());
		
		RunningCommandCreater runningCommand=taskScheduleCommanderExecute.getRunningCommandCreater();
		String pauseCommand=runningCommand.getPauseCommand(runningId);
		
		runningCommand.sendCommand(pauseCommand, runningHost);
	}

	@Override
	public void regainRunning(String runningId) throws Exception {
		Running running=getRunning(runningId);
		Host runningHost=Host.fromPathGetHost(running.getRunNode());
		
		RunningCommandCreater runningCommand=taskScheduleCommanderExecute.getRunningCommandCreater();
		String regainCommand=runningCommand.getRegainCommand(runningId);
		
		runningCommand.sendCommand(regainCommand, runningHost);
	}

	@Override
	public void stopRunning(String runningId) throws Exception {
		Running running=getRunning(runningId);
		Host runningHost=Host.fromPathGetHost(running.getRunNode());
		
		RunningCommandCreater runningCommand=taskScheduleCommanderExecute.getRunningCommandCreater();
		String stopCommand=runningCommand.getStopCommand(runningId);
		
		runningCommand.sendCommand(stopCommand, runningHost);
	}

	@Override
	public RunningStatus getRunningStatus(String runningId) throws Exception {
		Running running=getRunning(runningId);
		
		int status=running.getRunningStatus();
		return RunningStatus.getRSByStatus(status);
	}

	/**根据runningId获取running
	 * @param runningId
	 * @return
	 * @throws RunningException 
	 */
	@Override
	public Running getRunning(String runningId) throws RunningException{
		Running running=superDao.getOneT(new Query(Criteria.where(Running.COLUMN_ID).is(runningId)), Running.class);
		if(null == running){
			throw new RunningException("运行 {} 不存在",runningId);
		}
		return running;
	}

}
